import pygame,  Utilities,  math
from pygame.locals import *
from Utilities import *

class Player(pygame.sprite.Sprite):
    def __init__(self,  SCREENSIZE):
        pygame.sprite.Sprite.__init__(self)
        self.resizeX = 25
        self.resizeY = 100
        self.base_image = load_image("flecha.bmp", -1)
        self.base_image = self.base_image.convert_alpha()
        self.base_image = pygame.transform.scale(self.base_image,  (self.resizeX, self.resizeY))
        self.image = self.base_image
        self.centerX,  self.centerY = SCREENSIZE.midbottom
        self.centerY = self.centerY - 15
        self.rect = self.image.get_rect()
        self.rect.centerx = self.centerX
        self.rect.centery = self.centerY
        self.rotate((self.centerX, self.centerY-100))
        
    def rotate(self,  mousePosition):
        x,  y = mousePosition
        leftSide = x < self.centerX
        catetoAdy = x - self.centerX
        catetoOp = y - self.centerY
        if catetoOp < 0:
            tan = math.fabs(catetoAdy) / math.fabs(catetoOp)
            angleRadians = math.atan(tan)
            if leftSide:
                self.rotateAngle = math.degrees(angleRadians)
            else:
                self.rotateAngle = -math.degrees(angleRadians)
        else:
            if leftSide:
                self.rotateAngle = 85
            else:
                self.rotateAngle = -85
        self.image = pygame.transform.rotate(self.base_image,  self.rotateAngle)
        self.image = self.image.convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.centerx = self.centerX
        self.rect.centery = self.centerY
    
    # Este update no hace mucho, pero si se dispara y no se mueve el mouse, la imagen del misil central desaparece. Entonces, este update lo que hace es mantener siempre esa imagen 
    # visible
    def update(self):
        self.rect = self.image.get_rect()
        self.rect.centerx = self.centerX
        self.rect.centery = self.centerY
            
    
